﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace JuLiao.Manage.MVC4.Areas.AgentCenter.Controllers
{
    public class TestController : Controller
    {
        // GET: AgentCenter/Test
        public ActionResult Index()
        {
            return View();
        }

        public class Request_Base
        {
            public int cmd = 0;
            
        }

        public class Response_Base
        {
            public int flag = 0;
            public string msg = "";
        }

        public class Item
        {
            /// <summary>
            /// SN
            /// </summary>
            public string id = "";
            /// <summary>
            /// 商品名称
            /// </summary>
            public string name = "";
            /// <summary>
            /// 需要的总数量
            /// </summary>
            public int count = 0;
            /// <summary>
            /// 状态
            /// </summary>
            public int status = 0;
            /// <summary>
            /// 型号规格
            /// </summary>
            public string model = "";
            /// <summary>
            /// 信息列表
            /// </summary>
            public string[] list = new string[] { };
        }

        public class Response_Read : Response_Base
        {
            /// <summary>
            /// 单据号
            /// </summary>
            public string oId = "";
            /// <summary>
            /// 单据类型
            /// </summary>
            public string oType = "";
            /// <summary>
            /// 仓库
            /// </summary>
            public string oWarehouse = "";
            /// <summary>
            /// 供应商/客户
            /// </summary>
            public string oSupplier = "";
            public List<Item> List = new List<Item>();
        }

        public class Requeste_Read : Request_Base
        {
            public string oId = "";
        }

        public class Response_Save : Response_Base
        {
            public string oId = "";
            
        }

        public class Requeste_Save : Request_Base
        {
            public string oId = "";
            public List<Item> List = new List<Item>();
        }

        public class Response_Login : Response_Base
        {
            public string oId = "";

        }

        public class Requeste_Login : Request_Base
        {
            public string account = "";
            public string sessionid = "";
        }



        public ActionResult Interface()
        {

            
            JavaScriptSerializer js = new JavaScriptSerializer();
            string s = "";//Tinder.Html.Request.InputStreamString();
            var b = js.Deserialize<Request_Base>(s);
            object r;
            switch (b.cmd)
            {
                case 1001://登录
                    Requeste_Login re1 = js.Deserialize<Requeste_Login>(s);

                    Response_Login rr1 = new Response_Login() { flag = 0, msg = s };
                    r = rr1;
                   
                    break;
                case 1002://读取
                    Requeste_Read re = js.Deserialize<Requeste_Read>(s);
                    Response_Read rr = new Response_Read() { flag = 0, msg = s, oId = re.oId };
                    rr.oType = "采购入库单/采购出库单";
                    rr.oWarehouse = "我是仓库";
                    rr.oSupplier = "我是供应商/客户";
                    rr.List.Add(new Item() {
                        id = "1122", name = "产品一", count = 5, model = "我是小号的"
                    });

                    rr.List.Add(new Item()
                    {
                        id = "1123",
                        name = "产品二",
                        count = 4,
                        model="我是大号的"
                    });

                    r = rr;
                    break;
                case 1003://保存
                    Requeste_Save re3 = js.Deserialize<Requeste_Save>(s);

                    string info = re3.List.Count + "_" ;
                    foreach (var d in re3.List)
                    {
                        info += "||" + string.Join(",", d.list);
                    }
                    

                    Response_Save rr3 = new Response_Save() { flag = 0, msg =info+ s, oId = re3.oId };
                    r = rr3;
                    break;
                case 9999://测试
                    System.Threading.Thread.Sleep(2000);
                    r = new { flag = 0, msg = s };
                    break;
                default:
                    r = new { flag = 0, msg = s };
                    break;
            }
            return Json(r,JsonRequestBehavior.AllowGet);
        }
    }
}